package com.ruoyi.web.controller.applet;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.BdmCartDto;
import com.ruoyi.system.domain.dto.BdmOrdersUploadDto;
import com.ruoyi.system.domain.vo.BdmCartVo;
import com.ruoyi.system.service.IBdmCartService;
import com.ruoyi.system.service.IDistributionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车Controller
 *
 * @author ruoyi
 * @date 2024-12-12
 */
@RestController
@RequestMapping("/applet/cart")
public class AppletCartController extends BaseController {

    @Autowired
    private IBdmCartService bdmCartService;

    @Autowired
    private IDistributionService iDistributionService;

    /**
     * 查询用户购物车
     */
    @GetMapping("/getUserCart")
    public TableDataInfo getUserCart() {
        Long userId = SecurityUtils.getUserId();
        List<BdmCartVo> list = bdmCartService.getUserCart(userId);
        return getDataTable(list);
    }

    /**
     * 加入购物车
     */
    @PostMapping("/saveUserCart")
    public AjaxResult saveUserCart(@RequestBody BdmCartDto dto) {
        Long userId = SecurityUtils.getUserId();
        dto.setUserId(userId);
        return success(bdmCartService.saveUserCart(dto));
    }

    /**
     * 增加购物车商品数量
     */
    @PostMapping("/plusUserCartNumber")
    public AjaxResult plusUserCartNumber(@RequestBody BdmCartDto dto) {
        Long userId = SecurityUtils.getUserId();
        dto.setUserId(userId);
        return success(bdmCartService.plusUserCartNumber(dto));
    }

    /**
     * 减少购物车商品数量
     */
    @PostMapping("/reduceUserCartNumber")
    public AjaxResult reduceUserCartNumber(@RequestBody BdmCartDto dto) {
        return success(bdmCartService.reduceUserCartNumber(dto));
    }

    /**
     * 清空购物车商品
     */
    @GetMapping("/emptyUserCart")
    public AjaxResult emptyUserCart() {
        Long userId = SecurityUtils.getUserId();
        return success(bdmCartService.emptyUserCart(userId));
    }

    /**
     * 删除购物车单品数量
     */
    @GetMapping("/deleteUserCartProduct")
    public AjaxResult deleteUserCartProduct(@RequestParam("id") Long id) {
        return success(bdmCartService.deleteBdmCartById(id));
    }

}
